<?php
/**
 * Author：晴天 QQ：1186258278
 * Creation：2020/3/19 18:10
 * Filename：jiuwu.php
 */

namespace extend;

use community;

class jiuwu extends Share
{
    /**
     * @param $data 用户账号密码数据
     * 获取登陆cookie
     */
    public static function LoginCookie($data)
    {
        mkdirs(ROOT . 'includes/extend/log/Cookie/');
        if (file_exists(ROOT . 'includes/extend/log/Cookie/' . md5($data['url'] . $data['username']) . '.cookie')) {
            $lock_dates = filemtime(ROOT . 'includes/extend/log/Cookie/' . md5($data['url'] . $data['username']) . '.cookie'); #文件创建时间
            $cookie = dirname(__FILE__) . '/log/Cookie/' . md5($data['url'] . $data['username']) . '.cookie';
            if ($lock_dates < (time() - 60)) {
                $Curl_url = $data['url'] . 'index.php?m=Home&c=User&a=login';
                $Post_User = ['username' => $data['username'], 'username_password' => $data['password'], 'user_remember' => 1, 'sendpass_username' => '', 'reg_username' => '', 'reg_password' => '', 'reg_sex' => 0, 'reg_qq' => '', 'code' => '', 'id' => $data['gid'], 'goods_type' => $data['tyid']];
                $data = self::CurlCookieObtain($Curl_url, 0, $Post_User, $cookie);
                $data = community::jiuwu_regular($data); #玖伍正则取出错误返回
                if ($data['info'] == '用户名或密码不正确！') {
                    @unlink($cookie);
                    dies(-1, '用户名或密码不正确！');
                }
            }
        } else {
            $cookie = dirname(__FILE__) . '/log/Cookie/' . md5($data['url'] . $data['username']) . '.cookie';
            $Curl_url = $data['url'] . 'index.php?m=Home&c=User&a=login';
            $Post_User = ['username' => $data['username'], 'username_password' => $data['password'], 'user_remember' => 1, 'sendpass_username' => '', 'reg_username' => '', 'reg_password' => '', 'reg_sex' => 0, 'reg_qq' => '', 'code' => '', 'id' => $data['gid'], 'goods_type' => $data['tyid']];
            $data = self::CurlCookieObtain($Curl_url, 0, $Post_User, $cookie);
            $data = community::jiuwu_regular($data); #玖伍正则取出错误返回
            if ($data['info'] == '用户名或密码不正确！') {
                @unlink($cookie);
                dies(-1, '用户名或密码不正确！');
            }
        }

        return $cookie;
    }

    public static function TakeOutTheData($data)
    {
        $Cookie = self::LoginCookie($data);
        $CurlHtml = $data['url'] . '/index.php?m=home&c=goods&a=detail&id=' . $data['gid'] . '&goods_type=' . $data['tyid'];
        $HtmlData = self::CurlCookieObtain($CurlHtml, $Cookie);

        $regex = "/<span.*?class=\".*?fixed-width-right-80.*?\".*?>(.*?)<\/span>/is";
        preg_match_all($regex, $HtmlData, $result);

        $datar_arr = array();
        foreach ($result[1] as $v) {
            if ($v == '下单件数：' || $v == '下单天数：' || $v == '下单点数：' || $v == '下单粉丝：' || $v == '快手链接：' || $v == '下单评论：' || $v == 'K歌链接：' || $v == '下单赞：' || $v == '作品链接：' || $v == '下单喜欢：' || $v == '下单方式：' || $v == '作品链接：' || $v == '支付方式：' || $v == '接口：' || $v == '说说：' || $v == '操作备注：' || $v == '设置密码：' || $v == '卡密面值：' || $v == '输入卡密：' || $v == '支付方式：' || $v == '&nbsp;' || $v == '注意：') unset($v);
            if (strstr($v, '量') !== false) {
                unset($v);
            }
            if ($v) {
                $v = mb_substr($v, 0, -1);
                $datar_arr[] = $v;
            }
        }

        $regex = "/<div.*?class=\".*?col-md-12 banner.*?\".*?>(.*?)<\/div>/is";
        preg_match_all($regex, $HtmlData, $docs);

        $input_arr = array_unique($datar_arr);
        $docs = str_replace(["\n", "\r","\t"], "", $docs[1][0]);
        $pipei = self::Pipei($HtmlData)['param'];

        return [
            'inpus' => $input_arr,
            'pipei' => $pipei,
            'docs' => $docs,
        ];
    }

    public static function Pipei($HtmlData)
    {
        $DataPi = strpos($HtmlData, "action=\"/index.php?m=home&c=order");
        $pay_type = strpos($HtmlData, "name=\"pay_type");
        if ($DataPi > 1 && $pay_type > 1) {
            $DataName = substr($HtmlData, $DataPi, $pay_type - $DataPi);
            if (preg_match_all("/name=\"([a-z0-9A-Z\\_\\-]+)\"/is", $DataName, $DataArr)) {
                $val_arr = '';
                foreach ($DataArr[1] as $key => $val) {
                    if ($val != "need_num_0" && $val != "goods_id" && $val != "goods_type" && $val != "ssnr" && $val != "qmkg_url" && $val != "kszp_url" && $val != "kszy_url" && $val != "kszp_dwz") {
                        $val_arr = $val_arr . (trim($val) . ",");
                    }
                }
                $val_arr = trim($val_arr, ",");
                $Data = ["code" => 1, "message" => "succ", "param" => $val_arr];
            } else {
                $Data["code"] = -1;
                $Data["param"] = "匹配商品POST数据失败,重新尝试即可";
            }
        } else {
            $Data["code"] = -1;
            $Data["param"] = "获取商品POST数据失败,重新尝试即可";
        }
        return $Data;
    }
}